package com.sd.view;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.os.Bundle;

import com.alex.http.AHandleable;
import com.alex.http.AHandledResult;
import com.alex.http.AHttpRequest;
import com.alex.log.ALog;

public class DoubanHandler implements AHandleable {

	static final String ITEM = "item";
	private int currentstate = -1;
	final int TITLE = 1;
	final int LINK = 2;
	final int DESCRIPTION = 3;
	final int PUBDATE = 4;

	@Override
	public AHandledResult handle(long requestId, String paramString,
			Bundle paramBundle) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AHandledResult handle(long requestId, InputStream paramInputStream,
			Bundle paramBundle) {
		// TODO Auto-generated method stub
		XmlPullParserFactory xmlPullParserFactory;
		XmlPullParser xmlPullParser = null;
		try {
			xmlPullParserFactory = XmlPullParserFactory.newInstance();
			xmlPullParserFactory.setNamespaceAware(true);
			xmlPullParser = xmlPullParserFactory.newPullParser();
		} catch (XmlPullParserException e1) {
			e1.printStackTrace();
		}

		String name = "name";
		boolean isItemTAG = false;
		try {
			xmlPullParser.setInput(paramInputStream, "utf-8");

			int eventType = xmlPullParser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {

				if (eventType == XmlPullParser.START_DOCUMENT) {
					System.out.println("start Document...");
				} else if (eventType == XmlPullParser.END_DOCUMENT) {
					System.out.println("end Document...");
				} else if (eventType == XmlPullParser.START_TAG) {
					if (xmlPullParser.getName().equals("title")) {
						currentstate = TITLE;
					}
				} else if (eventType == XmlPullParser.END_TAG) {
				} else if (eventType == XmlPullParser.TEXT) {
					if (currentstate == TITLE) {
						currentstate = -1;
						name = xmlPullParser.getText();
					}

				}

				eventType = xmlPullParser.next();

			}

		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new AHandledResult(null, null, name);
	}

	@Override
	public int getContentType() {
		// TODO Auto-generated method stub
		return AHttpRequest.TYPE_STREAM;
	}

}